<template>
  <!-- 弹框 -->
  <el-dialog :visible.sync="dialogShow" :title="title" @close="onClose">
    <!-- 表单区域 -->
    <el-form
      label-width="140px"
      :model="formData"
      :rules="formRules"
      ref="form"
    >
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="角色描述" prop="description">
        <el-input
          type="textarea"
          :rows="3"
          v-model="formData.description"
          placeholder="请输入"
        ></el-input>
      </el-form-item>
      <el-form-item label="权限标识">
        <!-- 树 -->
        <el-tree
          :data="allTreeData"
          :props="{ label: 'title' }"
          show-checkbox
          @check="checkTree"
          node-key="id"
          ref="tree"
        ></el-tree>
      </el-form-item>
      <el-row type="flex" justify="center">
        <el-button type="primary" size="small" @click="onSubmit"
          >提交</el-button
        >
        <el-button size="small" @click="dialogShow = false">关闭</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>
<script>
import { addSysRoleInfoApi, updateSysRoleInfoApi } from "@/api/sys_role";
import { getSysMenuListApi } from "@/api/sys_menu";
export default {
  name: "addForm",
  data() {
    return {
      //弹框控制变量
      dialogShow: false,
      // 表单数据变量
      formData: {
        name: "",
        description: "",
        menuids: [],
      },
      // 表单验证规则
      formRules: {
        name: [{ required: true, message: "请输入", trigger: "blur" }],
        description: [{ required: true, message: "请输入", trigger: "blur" }],
      },
      // 树的数据
      allTreeData: [],
    };
  },
  computed: {
    title() {
      return this.formData.id ? "修改" : "添加";
    },
  },
  created() {
    this.getTreeData();
  },
  methods: {
    // 获取树的数据
    async getTreeData() {
      let res = await getSysMenuListApi();
      this.allTreeData = res;
    },
    // 树的勾选框事件
    checkTree(nodeData, data) {
      // data.checkedKeys 对勾的节点ID   data.halfCheckedKeys 半钩节点ID
      this.formData.menuids = [...data.checkedKeys, ...data.halfCheckedKeys];
    },
    // 提交行为
    async onSubmit() {
      // 提交之前获取一下树上的对钩和半钩
      this.formData.menuids = [
        ...this.$refs.tree.getCheckedKeys(),
        ...this.$refs.tree.getHalfCheckedKeys(),
      ];
      if (!this.formData.menuids.length) {
        return this.$message.warning("请勾选");
      }
      // 表单的校验
      await this.$refs.form.validate();
      // 加工处理表单数据(拷贝一份再加工)
      // 请求的发生
      this.formData.id
        ? await updateSysRoleInfoApi(this.formData)
        : await addSysRoleInfoApi(this.formData);
      // 提示效果
      this.$message.success(this.formData.id ? "修改成功" : "添加成功");
      // 关闭弹框
      this.dialogShow = false;
      // 列表更新
      this.$emit("updateList");
    },
    // 弹框关闭
    onClose() {
      // 表单重置
      this.formData = {
        name: "",
        description: "",
        menuids: [],
      };
      // 移除校验
      this.$refs.form.clearValidate();
      // 打钩也不要了
      this.$refs.tree.setCheckedKeys([]);
    },
  },
};
</script>
<style scoped></style>
